<template>
  <smart-page>
    <template #actions>
      <a-button v-auth="Acl.SYSTEM_SCHEDULE_TASK_DELETE" :disabled="selectedKeys.length === 0" @click="onDelete()">批量删除</a-button>
      <a-button v-auth="Acl.SYSTEM_SCHEDULE_TASK_ADD" type="primary" @click="editCtr.open()">新建任务</a-button>
    </template>
    <template #search>
      <smart-search :query-array="TASK_SEARCH" @on-handler="onSearch"/>
    </template>
    <a-table v-model:selectedKeys="selectedKeys"
             :table-layout-fixed="theme.layoutFixed"
             :row-selection="rowSelection"
             :columns="TASK_COLUMNS"
             :row-key="rowKey"
             :column-resizable="theme.resizable"
             :bordered="theme.bordered"
             :stripe="theme.stripe"
             :sticky-header="theme.sticky"
             :hoverable="theme.hoverAble"
             :pagination="pagination"
             :loading="theme.loading"
             :data="dataSource"
             @page-change="onPageChange"
             @page-size-change="onPageSizeChange">
      <template #action="{ record }">
        <a-space size="mini">
          <a-link v-auth="Acl.SYSTEM_SCHEDULE_RECORD_PAGE_LIST" @click="toRecord(record)"> 日志</a-link>
          <a-link v-auth="Acl.SYSTEM_SCHEDULE_TASK_RUN_NOW" @click="onRunNow(record)"> 执行</a-link>
          <a-link v-auth="Acl.SYSTEM_SCHEDULE_TASK_UPDATE" @click="editCtr.open(record)"> 编辑</a-link>
          <a-link v-auth="Acl.SYSTEM_SCHEDULE_TASK_DELETE" status="danger" @click="onDelete(record)"> 删除</a-link>
        </a-space>
      </template>
    </a-table>
    <edit-modal v-if="editCtr.show" :record="editCtr.record" @cancel="editCtr.close"/>
  </smart-page>
</template>
<script lang="ts" setup name="system-schedule-task">
import {onMounted} from 'vue';
import {TASK_SEARCH} from '../resource/search'
import {TASK_COLUMNS} from "../resource/colums";
import {TablePage} from "@/libs/core";
import {MsgUtil, RouterUtil} from "@/utils";
import {SystemHttp} from "../resource/http";
import EditModal from './edit.vue'
import {Acl} from "@/libs/acl";


const pageConfig = {title: '任务', rowKey: 'id', nameKey: 'title'}
const {getScheduleTaskPage, delTask, runNow} = SystemHttp
const tablePage = new TablePage(pageConfig).setFetchXhr(getScheduleTaskPage)
    .setDeleteXhr(delTask).setPermission(Acl.SYSTEM_SCHEDULE_TASK_PAGE_LIST);

const {theme, pagination, rowKey, dataSource, rowSelection, selectedKeys, editCtr} = tablePage
const {onFetch, onPageChange, onPageSizeChange, onDelete, onSearch} = tablePage
onMounted(() => onFetch())


const onRunNow = (record: { title: any; id: string | number; }) => {
  const tips = `确定要立即执行${record.title}这个任务吗?`
  const handler = async () => {
    const {message} = await runNow(record.id)
    MsgUtil.notify("操作成功", message)
    await onFetch()
  }
  MsgUtil.confirm(tips, handler)
}
const toRecord = (record: { id: any; }) => RouterUtil.toLink(`/system/schedule/record`, {taskId: record.id})
</script>

